﻿namespace HiMap.MapControls
{
    using HiMap.MapControls.Tools;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class VScrollToolboxControl : UserControl
    {
        private MapControl _Map;
        private bool beginmove;
        private double ButtonToToolboxPixels;
        private IContainer components;
        private List<MeTool> listTool;
        private int mouseY;
        private Panel panBack;
        private Panel panMain;
        private Panel panMove;
        private Panel panToolsMain;
        private PictureBox picBottomMenu;
        private PictureBox picButton;
        private PictureBox picMove;
        private PictureBox picSelected;
        private PictureBox picToolsBack;

        public VScrollToolboxControl()
        {
            this.InitializeComponent();
            this.Ini();
        }

        public void AddTool(ICommand Cmd, string Caption, Image Img, Image SelectedImg)
        {
            MeTool tool = new MeTool();
            tool.Cmd = Cmd;
            tool.Caption = Caption;
            tool.Img = Img;
            tool.SelectedImg = SelectedImg;
            tool.Type = "Command";
            this.listTool.Add(tool);
        }

        public void AddTool(ITool Cmd, string Caption, Image Img, Image SelectedImg)
        {
            MeTool tool = new MeTool();
            tool.Cmd = Cmd;
            tool.Caption = Caption;
            tool.Img = Img;
            tool.SelectedImg = SelectedImg;
            tool.Type = "Tool";
            this.listTool.Add(tool);
        }

        private void CalculatedControls()
        {
            this.panToolsMain.Height = this.listTool.Count * 30;
            this.panMain.Location = new Point((base.Location.X + base.Width) + 3, base.Location.Y + base.Height);
            this.panMain.Width = 30;
            this.panMain.Height = 0;
            this.panMain.Visible = true;
            base.Parent.Controls.Add(this.panMain);
            this.panMain.BringToFront();
            if (this.panToolsMain.Height < base.Height)
            {
                this.panToolsMain.Height = base.Height;
            }
            this.ButtonToToolboxPixels = Convert.ToDouble(this.panToolsMain.Height) / Convert.ToDouble((int) (this.panMove.Height - this.picButton.Height));
        }

        private void CreatePic(MeTool tl, int Index)
        {
            PictureBox box = new PictureBox();
            box.Name = "pic" + Index.ToString();
            box.Dock = DockStyle.Top;
            box.Width = 30;
            box.Height = 30;
            box.SizeMode = PictureBoxSizeMode.StretchImage;
            box.Visible = true;
            box.Click += new EventHandler(this.PicClick);
            box.Image = tl.Img;
            this.panToolsMain.Controls.Add(box);
            box.BringToFront();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void Ini()
        {
            this.panMain = new Panel();
            this.panMain.Visible = false;
            this.panToolsMain = new Panel();
            this.panToolsMain.Width = 30;
            this.panToolsMain.Height = 0;
            this.panToolsMain.Visible = true;
            this.panToolsMain.Location = new Point(0, 0);
            this.panMain.Controls.Add(this.panToolsMain);
            this.panToolsMain.BringToFront();
            this.picToolsBack = new PictureBox();
            this.picToolsBack.BackColor = Color.White;
            this.picToolsBack.Dock = DockStyle.Fill;
            this.picToolsBack.Visible = true;
            this.panToolsMain.Controls.Add(this.picToolsBack);
            this.listTool = new List<MeTool>();
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(VScrollToolboxControl));
            this.panBack = new Panel();
            this.panMove = new Panel();
            this.picButton = new PictureBox();
            this.picMove = new PictureBox();
            this.picBottomMenu = new PictureBox();
            this.panBack.SuspendLayout();
            this.panMove.SuspendLayout();
            base.SuspendLayout();
            this.panBack.Controls.Add(this.panMove);
            this.panBack.Controls.Add(this.picBottomMenu);
            this.panBack.Dock = DockStyle.Fill;
            this.panBack.Location = new Point(0, 0);
            this.panBack.Name = "panBack";
            this.panBack.Size = new Size(0x10, 150);
            this.panMove.Controls.Add(this.picButton);
            this.panMove.Controls.Add(this.picMove);
            this.panMove.Dock = DockStyle.Fill;
            this.panMove.Location = new Point(0, 0x10);
            this.panMove.Name = "panMove";
            this.panMove.Size = new Size(0x10, 0x86);
            this.picButton.Image = (Image) manager.GetObject("picButton.Image");
            this.picButton.Location = new Point(0, 0);
            this.picButton.Name = "picButton";
            this.picButton.Size = new Size(0x10, 30);
            this.picButton.SizeMode = PictureBoxSizeMode.StretchImage;
            this.picButton.MouseMove += new MouseEventHandler(this.picButton_MouseMove);
            this.picButton.MouseDown += new MouseEventHandler(this.picButton_MouseDown);
            this.picButton.MouseUp += new MouseEventHandler(this.picButton_MouseUp);
            this.picMove.Dock = DockStyle.Fill;
            this.picMove.Location = new Point(0, 0);
            this.picMove.Name = "picMove";
            this.picMove.Size = new Size(0x10, 0x86);
            this.picMove.SizeMode = PictureBoxSizeMode.StretchImage;
            this.picBottomMenu.Dock = DockStyle.Top;
            this.picBottomMenu.Image = (Image) manager.GetObject("picBottomMenu.Image");
            this.picBottomMenu.Location = new Point(0, 0);
            this.picBottomMenu.Name = "picBottomMenu";
            this.picBottomMenu.Size = new Size(0x10, 0x10);
            this.picBottomMenu.SizeMode = PictureBoxSizeMode.StretchImage;
            base.AutoScaleDimensions = new SizeF(96f, 96f);
            base.AutoScaleMode = AutoScaleMode.Dpi;
            base.Controls.Add(this.panBack);
            base.Name = "VScrollToolboxControl";
            base.Size = new Size(0x10, 150);
            this.panBack.ResumeLayout(false);
            this.panMove.ResumeLayout(false);
            base.ResumeLayout(false);
        }

        private void MoveTools()
        {
            if (Convert.ToInt32((double) (Convert.ToDouble(this.picButton.Location.Y) * this.ButtonToToolboxPixels)) >= base.Height)
            {
                this.panMain.Height = base.Height;
                this.panMain.Location = new Point(this.panMain.Location.X, base.Location.Y);
                this.panToolsMain.Location = new Point(this.panToolsMain.Location.X, base.Height - Convert.ToInt32((double) (Convert.ToDouble(this.picButton.Location.Y) * this.ButtonToToolboxPixels)));
            }
            else
            {
                this.panToolsMain.Location = new Point(this.panToolsMain.Location.X, 0);
                this.panMain.Height = Convert.ToInt32((double) (Convert.ToDouble(this.picButton.Location.Y) * this.ButtonToToolboxPixels));
                this.panMain.Location = new Point(this.panMain.Location.X, (base.Height + base.Location.Y) - this.panMain.Height);
            }
        }

        private void picButton_MouseDown(object sender, MouseEventArgs e)
        {
            this.beginmove = true;
            this.mouseY = e.Y;
        }

        private void picButton_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.beginmove)
            {
                int y = this.mouseY - e.Y;
                if ((this.picButton.Location.Y - y) > (this.panMove.Height - this.picButton.Height))
                {
                    y = this.panMove.Height - this.picButton.Height;
                }
                else if ((this.picButton.Location.Y - y) < 0)
                {
                    y = 0;
                }
                else
                {
                    y = this.picButton.Location.Y - y;
                }
                this.picButton.Location = new Point(this.picButton.Location.X, y);
                this.MoveTools();
            }
        }

        private void picButton_MouseUp(object sender, MouseEventArgs e)
        {
            this.beginmove = false;
        }

        private void PicClick(object sender, EventArgs e)
        {
            PictureBox box = (PictureBox) sender;
            MeTool tool = this.listTool[Convert.ToInt32(box.Name.ToString().Substring(3))];
            box.Image = tool.SelectedImg;
            box.Refresh();
            if (tool.Type == "Command")
            {
                ICommand cmd = (ICommand) tool.Cmd;
                cmd.OnCreate(this.Map);
                cmd.RunCommandControl = box;
                cmd.OnClick();
                box.Image = tool.Img;
                box.Refresh();
            }
            else
            {
                if (this.picSelected != null)
                {
                    MeTool tool2 = this.listTool[Convert.ToInt32(this.picSelected.Name.ToString().Substring(3))];
                    this.picSelected.Image = tool2.Img;
                    box.Refresh();
                }
                ITool tool3 = (ITool) tool.Cmd;
                tool3.OnCreate(this.Map);
                this.Map.CurTool = tool3;
                this.picSelected = (PictureBox) sender;
            }
        }

        public void ShowTools()
        {
            for (int i = 0; i < this.listTool.Count; i++)
            {
                this.CreatePic(this.listTool[i], i);
            }
            this.CalculatedControls();
        }

        public MapControl Map
        {
            get
            {
                return this._Map;
            }
            set
            {
                this._Map = value;
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct MeTool
        {
            public object Cmd;
            public string Caption;
            public Image Img;
            public Image SelectedImg;
            public string Type;
        }
    }
}

